var ge = 0;
var ge1 = 0;
var create_num = 1000;
var move_num = 200;
var guoguan_num = 10;
var shibai_num = 20;
var guanqia = 0;
var sc = setInterval("", 3000);
var yd = setInterval("", 500);
var sj = setInterval("", 1000);
var zqlv = 0;
window.onkeydown = function(event) {
	var k = event.keyCode;
	var div = document.getElementById("div");
	var p = div.children;
	for(i = 0; i < p.length; i++) {
		if(p[i].getAttribute("neirong") == k) {
			div.removeChild(p[i]);

			ge1++;
			if(ge1 == guoguan_num) {
				guanqia++;
				if(guanqia == 6) {
					alert("您已通关！");
					jieshu();
				} else {
					alert("闯关成功");
					chenggong();
				}
			}
			break;
		}
	}
	zql();
	sd();
	basket();
}

function kaishi() {
	clearInterval(sc);
	clearInterval(yd);
	clearInterval(sj);
	createPG();
	movePG();
	jishi();
	sc = setInterval("createPG()", create_num);
	yd = setInterval("movePG()", move_num);
	sj = setInterval("jishi()", 1000);
	zql();
	sd();
}

function createPG() {
	$("#div").append("<div></div>");
	var ping = $("#div div").last();
	var st = parseInt(Math.random() * 10) + 5;
	ping.attr("sudu", st);
	var num = parseInt(Math.random() * 26 + 65);
	ping.html("&#" + num + ";");
	ping.attr("neirong", num);
	ping.css("top", "0px");
	var left = parseInt(Math.random() * 717);
	ping.css("left", left)
}

function movePG() {
	var $div = $("#div div");
	var pgs = $div;
	for(var i = 0; i < pgs.length; i++) {
		var p = parseInt(pgs.eq(i).attr("sudu"));
		var top1 = parseInt(pgs.eq(i).css("top"));
		top1 += p;
		pgs.eq(i).css("top", top1);
		if(top1 > 360) {
			pgs.eq(i).css("width", 138).css("heigh t", 76).css("background-image", "url(img/破裂苹果.png)");
			pgs.eq(i).html("");
		}
		if(top1 > 380) {
			pgs.eq(i).remove();
			zql();
			sd();
			ge++;
			$("#shibai").html(ge);
			if(ge == shibai_num) {
				alert("失败");
				jieshu();
			}
		}
	}
}
var h = 0;
var m = 0;
var s = 0;

function jishi() {
	s++;
	if(s == 60) {
		m++;
		s = 1;
	}
	if(m == 60) {
		h++;
		m = 0;
	}
	var $time = $("#shijian");
	$time.html((h - 10 > 0 ? h : "0" + h) + ":" + (m - 10 > 0 ? m : "0" + m) + ":" + (s - 10 >= 0 ? s : "0" + s));
}

function zql() {
	if(ge + ge1 == 0) {
		zqlv = 0;
	} else {
		zqlv = parseInt((ge1 / (ge + ge1)) * 100);
	}
	var z = $("#zql");
	z.html(zqlv + "%");
}

function sd() {
	var shijian = h * 60 + m + s / 60;
	console.log()
	var sd = ge1 / shijian;
	var sudu = $("#sudu");
	if(shijian == 0) {
		sudu.html("0个/分钟");
	} else {
		sudu.html(parseInt(sd) + "个/分钟");
	}

}

function zanting() {
	clearInterval(sc);
	clearInterval(yd);
	clearInterval(sj);
}

function jieshu() {
	var ping = $("#div");
	ping.html("");
	clearInterval(sc);
	clearInterval(yd);
	clearInterval(sj);
	s = 0;
	m = 0;
	h = 0;
	var time = $("#shijian");
	time.html("00:00:00");
	var z = $("#zql");
	ge = 0;
	ge1 = 0;
	zqlv = 0;
	z.html(zqlv + "%");
	var sudu = $("#sudu");
	sudu.html("0个/分钟");
	$("#shibai").html("0");
	$("#basket div").hide();
	create_num = 1000;
	move_num = 200;
	guoguan_num = 100;
	shibai_num = 20;
	guanqia = 0;
}

function basket() {
	var g = parseInt(ge1 / (guoguan_num / 10));
	switch(g) {
		case 1:
			$("#basket_apple1").show();
			break;
		case 2:
			$("#basket_apple2").show();
			break;
		case 3:
			$("#basket_apple3").show();
			break;
		case 4:
			$("#basket_apple4").show();
			break;
		case 5:
			$("#basket_apple5").show();
			break;
		case 6:
			$("#basket_apple6").show();
			break;
		case 7:
			$("#basket_apple7").show();
			break;
		case 8:
			$("#basket_apple8").show();
			break;
		case 9:
			$("#basket_apple9").show();
			break;
		default:
			break;
	}
}

function shezhi() {
	zanting();
	$("#she_zhi").show();
}

function dengji() {
	var n = $("#dengji").val();
	$("#guoguan_shuliang").html((n - 1) * 20 + 10);
	$("#shibai_shuliang").html(20 - 2 * (n - 1));
}

function moren() {
	$("#dengji").val("1");
	$("#guoguan_shuliang").html(10);
	$("#shibai_shuliang").html(20);
}

function queding() {
	guoguan_num = $("#guoguan_shuliang").html();
	shibai_num = $("#shibai_shuliang").html();
	jieshu();
	$("#she_zhi").hide();
}

function quxiao() {
	$("#she_zhi").hide();
}

function chenggong() {
	create_num = 1000 - guanqia * 100;
	move_num = 200 - guanqia * 20;
	$("#div").html("");
	$("#basket div").hide();
	clearInterval(sc);
	clearInterval(yd);
	clearInterval(sj);
	s = 0;
	m = 0;
	h = 0;
	var time = $("#shijian");
	time.html("00:00:00");
	var z = $("#zql");
	ge = 0;
	ge1 = 0;
	zqlv = 0;
	z.html(zqlv + "%");
	var sudu = $("#sudu");
	sudu.html("0个/分钟");
	$("#shibai").html("0");
	kaishi();
}